package top.shiyiri.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

/**
 * @author Aunean
 * @date 2022/2/9 15:52
 */
public class RegisterServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取用户名
        String username = req.getParameter("username");

        //获取Session中的验证码
        String token = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        //删除Session中的验证码
        req.getSession().removeAttribute(KAPTCHA_SESSION_KEY);
//        req.getRequestDispatcher("/ok.jsp").forward(req, resp);

/*        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        String code = req.getParameter("code");
        if (null != token && token.equalsIgnoreCase(code)) {
            resp.sendRedirect("ok.jsp");
            System.out.println("保存到数据库：" + username);
        } else {
            System.out.println("请不要重复提交表单");
        }
    }
}
